# 返回的是函数 内部
# 函数引用了 外部函数 的变量值
#
from functools import reduce


def func(b):
    a = 100

    def inner():
        s = a + b
        print(s)

    return inner


x = func(20)

print(x())

# 作用


# 匿名函数
# 格式 lambda  param , param ... :  运算
s = lambda a, b: a + b
print(s)
# <function <lambda> at 0x10eab2a70>

print(s(10, 20))


# 匿名函数作为 参数

def funcnmcs(x, y, func):
    """
    文档说明格式：
    :param x:
    :param y:
    :param func:
    :return:
    """
    print(x, y)
    print(func)
    s = func(x, y)
    print(s)


print('*' * 50)

funcnmcs(10, 20, lambda a, b: a * b)

list11 = [1, 2, 3, 4, 5, 6, 7]
funclist = lambda x: x if x % 2 == 0 else x + 1

result = funclist(5)
print(result)

result = map(lambda x: x if x % 2 == 0 else x + 1, list11)

print(result)
print(list(result))


def square(x):  # 计算平方数
    ...
    return x ** 2


# ...
map(square, [1, 2, 3, 4, 5])  # 计算列表各个元素的平方
# [1, 4, 9, 16, 25]
map(lambda x: x ** 2, [1, 2, 3, 4, 5])  # 使用 lambda 匿名函数
# [1, 4, 9, 16, 25]
#
# # 提供了两个列表，对相同位置的列表数据进行相加
map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])


# [3, 7, 11, 15, 19]


def add(x, y):
    return x + y


"""
    reduce(function, sequence[, initial]) -> value

    Apply a function of two arguments cumulatively to the items of a sequence,
    from left to right, so as to reduce the sequence to a single value.
    For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
    ((((1+2)+3)+4)+5).  If initial is present, it is placed before the items
    of the sequence in the calculation, and serves as a default when the
    sequence is empty.
"""
# 再看reduce的用法。reduce把一个函数作用在一个序列[x1, x2, x3, ...]上，
# 这个函数必须接收两个参数，reduce把结果继续和序列的下一个元素做累积计算，其效果就是：
# reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)
print('&' * 80)
print('&' * 80)

print(reduce(add, range(1, 101)))  # 累加函数


"""

mode :
 r   w    纯文本
 
     rb      pic        read binary
     wb      map map4   write binary
     
     read  in
     write out 
     
 buffer:
     cache
      

file, mode='r', buffering=None, encoding=None, errors=None, newline=None, closefd=True
"""

print('^' * 150)
print('^' * 150)
print('^' * 150)
print('^' * 150)
print('^' * 150)
stream = open(r'/Users/liangbo/PycharmProjects/untitled/SUIBI001.md')

container = stream.read()
print(container)
